package com.azt.provider.impl;

import com.azt.api.pojo.*;
import com.azt.api.service.MsgUserService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.*;
import com.azt.utils.CommonUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MsgUserServiceImpl implements MsgUserService {
	
	@Autowired
	private MsgUserMapper msgUserMapper;
	
	@Autowired
	private MsgUserUnsendMapper msgUserUnsendMapper;
	
	@Autowired
	private MsgConfigMapper msgConfigMapper;
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	@Autowired
	private MsgEnquirySendMapper msgEnquirySendMapper;

	@Autowired
	private MsgEnquirySendMapper mesm;

	@Override
	public Pagination selectMsgUserPage(int pageNo, int pageSize, Map<String, String> params) {
		Page<Object> startPage=PageHelper.startPage(pageNo, pageSize);
		List<MsgUser> msgUserList=msgUserMapper.selectMainMsgInfo(params);
		Pagination p=new Pagination(startPage, msgUserList);
		p.setList(msgUserList);
		return p;
	}

	@Override
	public int selectMsgCountNotRead(Map<String, String> params) {
		return msgUserMapper.selectMsgCountNotRead(params);
	}

	@Override
	public void changeMsgReaded(MsgUser msg) {
		 msgUserMapper.updateByPrimaryKeySelective(msg);
	}

	@Override
	public void changeAlltoReaded(String[] msgids) {
		 for (int i = 0; i < msgids.length; i++) {
			MsgUser msg=new MsgUser();
			msg.setId(CommonUtil.safeToInteger(msgids[i]));
			msg.setIfRead(1);
			msgUserMapper.updateByPrimaryKeySelective(msg);
		 }
	}

	@Override
	public void delchooseMsg(String[] msgids) {
		 for (int i = 0; i < msgids.length; i++) {
			 MsgUser msg=new MsgUser();
		     msg.setId(CommonUtil.safeToInteger(msgids[i]));
		     msgUserMapper.deleteByPrimaryKey(msg);
		 }
	}

	@Override
	public List<String> selectNoReadMsgCountByUserid(Integer userid) {
		List<String> stringArr=new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			MsgUser msg=new MsgUser();
			msg.setUserId(userid);
			msg.setMsgType(i>0?i:null);
			msg.setIfRead(0);
			stringArr.add(String.valueOf(msgUserMapper.selectCount(msg)));
		}
		return stringArr;
	}
	
	@Override
	public int allCountNoReadMsg(Integer userId) {
		MsgUser msg=new MsgUser();
		msg.setUserId(userId);
		msg.setIfRead(0);
		msg.setIfMail(1);
		return msgUserMapper.selectCount(msg);
	}


	@Override
	public List<MsgUser> getDelayMsg() {
		MsgUser msg=new MsgUser();
		msg.setDelay(1);
		return msgUserMapper.select(msg);
	}

	@Override
	public void updateMsgUser(MsgUser msg) {
		msgUserMapper.updateByPrimaryKey(msg);
	}

	@Override
	public List<MsgConfig> selectMainMsgConfig(int userId,int roleType) {
		return msgConfigMapper.selectMainMsgConfig(userId,roleType);
	}

	@Override
	public List<Dictionary> selectMsgTypeDic() {
		Dictionary d=new Dictionary();
		d.setCode("msgType");
		Dictionary dic=dictionaryMapper.selectOne(d);
		Dictionary d2=new Dictionary();
		d2.setParentId(dic.getId());
		return dictionaryMapper.select(d2);
	}

	@Override
	public MsgUserUnsend getMagUserUnsendByUidAndCode(int uid, String code) {
		MsgUserUnsend muSend=new MsgUserUnsend();
		muSend.setUserId(uid);
		muSend.setMsgCode(code);
		return msgUserUnsendMapper.selectOne(muSend);
	}

	@Override
	public void addNewMsgUserUnsend(MsgUserUnsend unsend) {
		msgUserUnsendMapper.insertSelective(unsend);
	}

	@Override
	public void deleteMsgUserUnsend(Integer id) {
		msgUserUnsendMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateMsgUserUnsend(MsgUserUnsend unsend) {
		msgUserUnsendMapper.updateByPrimaryKeySelective(unsend);
	}

	@Override
	public List<MsgEnquiryUnsendVO> selectMainMsgEnquiry(int userId) {
		return msgEnquirySendMapper.selectMainMsgEnquiry(userId);
	}

	@Override
	public void addMsgEnquiryUnsend(MsgEnquirySend enqu) {
		 msgEnquirySendMapper.insertSelective(enqu);
	}

	@Override
	public MsgEnquirySend selectEnqUnsendByUidAndCid(int uid, int cid) {
		MsgEnquirySend enquirySend=new MsgEnquirySend();
		enquirySend.setUserId(uid);
		enquirySend.setCategoryId(cid);
		return msgEnquirySendMapper.selectOne(enquirySend);
	}

	@Override
	public void deleteEnqUnsendByUidAndCid(int id) {
		msgEnquirySendMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateEnqUnsend(MsgEnquirySend enqu) {
		 msgEnquirySendMapper.updateByPrimaryKeySelective(enqu);
	}

	@Override
	public void insertByUserAndCompany(Integer userId, Integer companyId) {
		MsgEnquirySend msgEnquirySend = new MsgEnquirySend();
		msgEnquirySend.setUserId(userId);
		List<MsgEnquirySend> sends = mesm.select(msgEnquirySend);
		if(CollectionUtil.isEmpty(sends)){
			mesm.insertByUserAndCompany(userId , companyId);
		}
	}

	@Override
	public List<Map> findHeadMsgCount(int userId) {
		return msgUserMapper.findHeadMsgCount(userId);
	}

}
